
var CalendarWeek = new Object();
var CalendarWeekGrid = new Object();
var CalendarWeekGridCell = new Object();



/* View */

CalendarWeek.createView = function(tasks) {
	var calendar = new WindView();
	
	var title = WindLabel.createView();
	title.setId("calendarWeekTitle");
	title.setCssClass("CalendarWeekTitle");
	title.setText("<< Semana del 11/4 al 17/4 >>");
	calendar.addView(title);

	var grid = CalendarWeekGrid.createView(tasks);
	grid.setId("calendarWeekGrid");
	calendar.addView(grid);
	
	return calendar;
}

CalendarWeekGrid.createView = function(tasks) {

	var gridViewModel = new Object();
	gridViewModel.getRows = function(){return 16};
	gridViewModel.getColumns = function(){return 8};

	var year = 2011;
	var month = 4;
	var firstDay = 11;
	var firstHour = 7;
	var firstMinute = 30;

	gridViewModel.getRowHeight = function(row) {
		height = 30;
		if(row==0) {
			height = 20;
		}
		return height;
	};

	gridViewModel.getColumnWidth = function(column) {
		width = 60;
		if(column==0) {
			width = 30;
		}
		return width;
	};

	gridViewModel.getCellAt = function(row, column) {
		var cell = null;
		if(row==0 && column>0) {
			var label = WindLabel.createView();
			label.setCssClass("CalendarWeekDate");
			var day = firstDay + column - 1;
			label.setText(day + "/" + month + "/" + year);
			cell = label;
		}
		if(column==0 && row>0) {
			var label = WindLabel.createView();
			label.setCssClass("CalendarWeekHour");
			var hour = firstHour + (row-1);
			if(hour<9) {
				hour = "0" + hour;
			}
			hour += ":00";
			
			label.setText(hour);
			cell = label;
		}
		if(row>0 && column>0) {
			var day = firstDay + column - 1;;
			var hour = firstHour + (row - 1);
			var minute = 0;
			var duration = 60;
			
			var cellStartDate = new Date();
			cellStartDate.setYear(year);
			cellStartDate.setMonth(month-1);
			cellStartDate.setDate(day+1);
			cellStartDate.setHours(hour);
			cellStartDate.setMinutes(minute);
			cellStartDate.setSeconds(0,0);
			var cellFinishDate = new Date();
			cellFinishDate.setTime(cellStartDate.getTime() + (duration * 60 * 1000));

			var columnStartDate = new Date();
			columnStartDate.setYear(year);
			columnStartDate.setMonth(month-1);
			columnStartDate.setDate(day+1);
			columnStartDate.setHours(0);
			columnStartDate.setMinutes(0);
			columnStartDate.setSeconds(0,0);
			var columnFinishDate = new Date();
			columnFinishDate.setTime(columnStartDate.getTime() + (24 * 60 * 60 * 1000));
			
			cell = CalendarWeekGridCell.createView(columnStartDate, columnFinishDate, cellStartDate, cellFinishDate, tasks);
		}
		return cell;
	}

	var gridView = WindGrid.createView(gridViewModel);
	
	return gridView;
}

// Cachea los anchos para las tareas
var CalendarWeekGridCellTaskCache = new Array();

function CalendarWeekGridCellTaskCacheDTO() {
}

CalendarWeekGridCell.createView = function(columnStartDate, columnFinishDate, cellStartDate, cellFinishDate, calendarDTO) {
	var cell = new WindView;
	cell.setCssClass("CalendarGridCell");

	var allTheTaskInTheColumn = calendarDTO.getTasksInInterval(columnStartDate, columnFinishDate);
	var headingTasksInTheColumn = new Array();
	for(var i=0; i<allTheTaskInTheColumn.length; i++) {
		var task = allTheTaskInTheColumn[i];
		if(task.getPreviousTask()==null) {
			headingTasksInTheColumn[headingTasksInTheColumn.length] = task;
		}
	}
	
	var cellTasks = calendarDTO.getTasksInInterval(cellStartDate, cellFinishDate);
	if(cellTasks.length>0) {
		
		var borderWidth = 0;
		
		var totalWidth = 60 - (borderWidth*2);
		var taskWidth = totalWidth / headingTasksInTheColumn.length; 
		var totalHeight = 30;
		
		for(var i=0; i<cellTasks.length; i++) {
			var task = cellTasks[i];
		
			var taskStartDate = task.getStartDate();
			var taskFinishDate = new Date();
			taskFinishDate.setTime(taskStartDate.getTime() + (task.getDuration() * 60 * 1000));
						
			var height = (totalHeight/60) * task.getDuration();
		
			var y = borderWidth;
			if(taskStartDate>=cellStartDate) {
				var millis = taskStartDate.getTime()-cellStartDate.getTime();
				var seconds = millis / 1000;
				var minutes = seconds / 60;
				y += (totalHeight/60)*(minutes);
			} 
			
			var height = totalHeight - y;
			if(taskFinishDate<=cellFinishDate) {
				var millis = cellFinishDate.getTime()-taskFinishDate.getTime();
				var seconds = millis / 1000;
				var minutes = seconds / 60;
				height = totalHeight - (totalHeight/60)*(minutes);
			}
			
			
			
			var cache = null;
			var k = 0;
			var found = false;
			while(!found && k<CalendarWeekGridCellTaskCache.length) {
				cache = CalendarWeekGridCellTaskCache[k];
				if(cache.task==task || cache.task.isParentTaskOf(task)) {
					found = true;
				} else {
					k++;
				}
			}
			
			var x = borderWidth*2;
			
			if(!found) {
			
				var j = 0;
				var found = false;
				while(!found && j<headingTasksInTheColumn.length) {
					if(task == headingTasksInTheColumn[j]) {
						found = true;
					} else {
						x += taskWidth;
						j++;
					}
				}
				
				cache = new CalendarWeekGridCellTaskCacheDTO;	
				cache.task = task;
				cache.x = x;
				cache.width = taskWidth;
				CalendarWeekGridCellTaskCache[CalendarWeekGridCellTaskCache.length] = cache;
			} else {
				x = cache.x;
				taskWidth = cache.width;
			}
		
			var viewTask = new WindView;
			viewTask.setId(task.getId());
			viewTask.setX(x);
			viewTask.setY(y);
			viewTask.setWidth(taskWidth);
			viewTask.setHeight(height);
			viewTask.setCssClass("CalendarGridCellTask");
			viewTask.setBackgroundColor(task.getColor());
			cell.addView(viewTask);
		}
	}
	
	return cell;
}
